/*
 * table_device.cpp
 *
 *  Created on: 12.06.2013
 *      Author: SpirTanol
 */

#include "ui/table_device.h"
#include "ui/models/device_model.h"
#include "device/device_info.h"

// QT
#include <QHeaderView>
#include <QMouseEvent>
#include <QMetaType>
//=========================================================================================
TableDevice::TableDevice(QVector<DeviceInfo*> *deviceList, QWidget *parent) : QTableView(parent),
	oCurrentDevice(0)
{
	qRegisterMetaType<DeviceInfo*>("DeviceInfo*");
	oModel = new DeviceModel(deviceList);
	setModel(oModel);
	verticalHeader()->hide();
	setEditTriggers(QAbstractItemView::NoEditTriggers);
	setColumnWidth(0, 280);
	//setColumnWidth(1, 120);
	setSelectionBehavior(QAbstractItemView::SelectRows);
}

TableDevice::~TableDevice()
{

}
//=========================================================================================
void TableDevice::currentChanged(const QModelIndex &current, const QModelIndex &previous)
{
	oCurrentDevice = static_cast<DeviceModel*>(model())->deviceOf(current);
	emit selectedDevice(oCurrentDevice);
}

void TableDevice::mouseDoubleClickEvent(QMouseEvent *event)
{
	QModelIndex index = indexAt(event->pos());
	DeviceInfo *dev = static_cast<DeviceModel*>(model())->deviceOf(index);
	if (dev && dev->type() != DT_UNKOWN)
		emit connectTo(dev);

}
//=========================================================================================
void TableDevice::addDevice()
{
	oModel->addItem();
}
//=========================================================================================
void TableDevice::clear()
{
	oModel->clear();
}
